//
//  DSUnitListController.m
//  DSJY
//
//  Created by 冯华强 on 16/9/28.
//  Copyright © 2016年 dsjy. All rights reserved.
//  单元列表 

#import "DSUnitListController.h"
#import "DSUnitExerciseController.h"
#import "UIStoryboard+Extension.h"
#import "MTNavigationController.h"
#import "DSUnitViewModel.h"
#import "DSUnitCell.h"
#import "DSCourse.h"
#import "DSUnit.h"
#import "MBProgressHUD+MJ.h"

@interface DSUnitListController ()
//首页VM
@property (nonatomic, strong) DSUnitViewModel *unitVM;
@end

@implementation DSUnitListController

#pragma mark - 懒加载
- (DSUnitViewModel *)unitVM
{
    if (_unitVM == nil) {
        _unitVM = [[DSUnitViewModel alloc] initWithBookId:self.course.courseId];
    }
    return _unitVM;
}

#pragma mark - 系统回调
- (void)viewDidLoad
{
    [super viewDidLoad];
    //显示backItem
    [self setupnavigationItem];
    
    //绑定事件
    [self bindEvent];
}

#pragma mark - 设置UI
- (void)setupnavigationItem
{
    self.showBackItem = YES;
}

#pragma mark - 设置事件
- (void)bindEvent
{
    @weakify(self);
    //学习通关后刷新单元列表
    [[[MTNotificationCenter rac_addObserverForName:DSLearnPassDidRefreshUnitListNote object:nil] takeUntil:self.rac_willDeallocSignal] subscribeNext:^(NSNotification *noti) {
        @strongify(self);
        //执行请求
        [self checkNetWorkWithRefreshType:YQRefreshTypeHeader];
    }];
    
    //如果验证通过，调用命令信号
    [self.unitVM.requestCommand.executionSignals.switchToLatest subscribeNext:^(id x) {
        @strongify(self);
        [self reloadDataWithCollectionView:self.collectionView currentPage:self.unitVM.pageNum totalPage:self.unitVM.response.total_page];
    }];
    
    //有网络
    [self.networkWiFiWWANSignal subscribeNext:^(RACTuple *tuple) {
        @strongify(self);
        // 执行请求
        [self.unitVM.requestCommand execute:tuple[0]];
    }];
    
    //无网络
    [self.networkUnknownNotReachableSignal subscribeNext:^(id x) {
        @strongify(self);
        [self showEmptyHUDWithView:self.collectionView];
    }];
    
    //执行请求
    [self checkNetWorkWithRefreshType:YQRefreshTypeHeader];
}

#pragma mark <UICollectionViewDataSource>
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    //数据一刷新就会调用这个方法，所以放在这里比较合适
    //    [UIScreen mainScreen].bounds.size 或者 CGSizeMake(self.collectionView.width, 0);都可以
    [self viewWillTransitionToSize:[UIScreen mainScreen].bounds.size];
    return self.unitVM.unitList.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    DSUnitCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:unitReuseIdentifier forIndexPath:indexPath];
    cell.unit = self.unitVM.unitList[indexPath.item];
    return cell;
}

#pragma mark <UICollectionViewDelegate>
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    DSUnit *unit = self.unitVM.unitList[indexPath.item];
    
    if (!unit.iscloseb || isDeBug) {
        DSUnitExerciseController *unitExerciseVC = (DSUnitExerciseController *)[UIStoryboard initialVCWithName:@"DSUnitExerciseController"];
        unitExerciseVC.appResourceAddressIp = self.unitVM.appResourceAddressIp;
        unitExerciseVC.unit = unit;
        MTNavigationController *navVC = [[MTNavigationController alloc] initWithRootViewController:unitExerciseVC];
//        navVC.autorotate = NO; //不允许旋转屏幕
        unitExerciseVC.title = [NSString stringWithFormat:@"%@ > %@", self.course.name, unit.name];
        unitExerciseVC.course = self.course;
        unitExerciseVC.unitList = self.unitVM.unitList;
        [self presentViewController:navVC animated:YES completion:nil];
    }else{ //暂时不做任何操作
        [MBProgressHUD showText:@"本单元开锁需要上一单元分数达90分或以上哦!" view:self.view];
    }
}

@end
